﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Drawing.Printing;
using WMS_BLL;
using WMS_DATABASE_MYSQL.MappingClass;
using WMS_DATABASE_MYSQL.MappingClass.CategoryClass;
using WMS_IBLL;
using WMS_WEBAPI.Controllers.Base;


namespace WMS_WEBAPI.Controllers.Table
{
    [ApiController]
    [Route("[controller]/[action]")]
    public class CategoryClassController : ControllerBase
    {
        private readonly ILogger<CategoryClassController> _logger;
        private readonly string controllerName;
        private readonly ICateGoryClassIBLL _cateGoryClassIBLL;
        public CategoryClassController(ILogger<CategoryClassController> logger, ICateGoryClassIBLL cateGoryClassIBLL)
        {
            _logger = logger;
            controllerName = GetType().Name;
            _cateGoryClassIBLL = cateGoryClassIBLL;
        }
        //以下为管理员及以上角色才能使用的方法
        //[Authorize(Roles = "superadmin,admin")]
        [HttpGet]
        public async Task<IActionResult> GetCategoryClassAsync(int current, int pageSize,int? category, string? description)
        {
            var datalist = await _cateGoryClassIBLL.QueryListAsync(a => (category == null || a.category == category) && (description == null || a.description == description));
            var datacount = datalist.Count();
            var data = datalist.Skip((current - 1) * pageSize).Take(pageSize);
            return Ok(new { data, total = datacount,suceess=true });
        }

        [HttpDelete]
        public async Task<IActionResult> DelCategoryClassAsync([FromBody] List<int> key)
        {
            var t=await _cateGoryClassIBLL.DeleteRangeAsync(a=>key.Contains(a.key));
            if (t > 0) {
                return Ok("成功删除");
            }
            return BadRequest();
        }

        [HttpPut]
        public async Task<IActionResult> EditCategoryClassAsync([FromBody] wms_categoryclass_table data)
        {
            var t = await _cateGoryClassIBLL.EditAsync(data);
            if (t)
            {
                return Ok("成功修改");
            }
            return BadRequest();
        }
        [HttpPost]
        public async Task<IActionResult> AddCategoryClassAsync([FromBody] wms_categoryclass_table data)
        {
            var t = await _cateGoryClassIBLL.CreateAsync(data);
            if (t)
            {
                return Ok("成功添加");
            }
            return BadRequest();

        }
    }
}
